package com.ruoyi.lessee.pay;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDlQsE1sPWMq7RBWYvxnGIXfYw8Zh8YX0Y5MHkF7D152rQbeEKL6Eo25qpH76SZsapdI9G1reDDr/iVo19nef9ptqXE12pmMX7hX6ze+zp9XIO7+DEHK3j3f7PnPNml7TR2bSUan+cDd/cW5Sx3szQCQeXPliQCeLoon+mV2xMpOt2pCMEWl818NAOk8IXpBEeGE8WdhZwXsMUQ+TEanW+LX19AfvJaPwtIVQ7AI9vdCekLwKFHPSkDB2GBRMlk1alu06FxEnrz/TpmEow+O/CijZI7Zq0I7LvLSQB1Ft9rPHHN/TiKBVZnZaMliWwGasijLA7N5sFOFW7jrV4lljjdAgMBAAECggEAHWKBATC9SFp5sCaaGU3CBODCqLsuISGVDp9345p0BrpRe3Pek7AiUwcdrDq7DjgHbgi5SGAQ/wP/CK73+3tXkWUnuiMiMBcH/DsXHT12Mbs5503YeuLHjnF/zZOvEeVlZAh5SQBVy8wEj5qwKGd/swpfOkw0MVzn/2z/s4Vw30e+FTXoM/rKayMtWkBCeV2IliUrjH1mgzK0u1LtGUXXxzaKEfqOg51+BakZ2tO4u9FZa+czUGlKCGKNHDdPxgd/QG05YeLnDXoMiTC854OEMN2QIQhgC2XXCkmw/ocTBSC7WFjzoZ0hMs7Dv/I139rBqzs3JwUF+uNANgjMrxS0AQKBgQD+BCqR1+Mgqxuoxz7aoFhwQHU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TS3qnsG3yXuVz71GbfcB21qxGSuR7nDG+9cfdZI+wFFtCNVstk5pisaNWKILPu5k/Ld/xZ03r7pWJn454p5nBJSsz9vZOCvTXDJsZKrv/RrPpKRwSxxbPMtkvtJ2DCmZGujtg7Zqy5oSHk8lr+IbInFtPOInOGG1tNkMST4v2aNQcdIdRCwwIDAQAB";

    //生成支付订单接口
    public String pay(Long id,Integer price) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122677189");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(id+"");
        model.setTotalAmount(price.toString());
        model.setSubject("427测试zfb");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
        request.setReturnUrl("http://127.0.0.1:9901/lessee/alipayResult");
        return response.getBody();
    }


    //查询接口
    public Boolean queryResult(Long tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipaydev.com/gateway.do");
        alipayConfig.setAppId("2021000122677189");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo+"");
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }
}
